package com.springboot.hotel.controller.hotelRoom;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.springboot.hotel.model.RoomType;
import com.springboot.hotel.service.impl.RoomTypeServiceImpl;
import com.springboot.hotel.xencryption.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

/*
* 房间类型控制器
 */
@RestController
@RequestMapping("/HotelRoom/RoomType")
public class RoomTypeController {
    @Autowired RoomTypeServiceImpl roomTypeService;

//    RoomTypeIndex房间类型首页
    @GetMapping("/RoomTypeIndex")
    public ModelAndView RoomListIndex(){
            ModelAndView mv = new ModelAndView("HotelRoom/RoomType/RoomTypeIndex");
            return mv;
    }

    /*表格数据显示*/
//    搜索框功能service实现
    @GetMapping("/roomTypeList")
    public R roomTypeList(@RequestParam(defaultValue = "1") Integer page,
                      @RequestParam(defaultValue = "5") Integer limit, RoomType roomType) {
        PageInfo<RoomType> pageInfo = roomTypeService.list(roomType, page, limit);
        return R.ok(pageInfo);
    }

//     编辑用户页面用户信息显示

    @GetMapping("/edit")
    public ModelAndView userdetail(Integer id) {
        QueryWrapper<RoomType> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        RoomType one = roomTypeService.getOne(wrapper);
        ModelAndView roomTypeMv = new ModelAndView("/HotelRoom/RoomType/edit", "type", one);
        return roomTypeMv;
    }


    //    编辑用户页面用户信息修改
    @PostMapping("/edit")
        public R edit(@RequestBody RoomType roomType) {
        boolean b = roomTypeService.updateById(roomType);
        if (b) {
            return R.ok("修改成功");
        } else {
            return R.fail("修改失败");
        }
    }

    //    添加用户页面
    @GetMapping("/add")
    public ModelAndView add() {
        ModelAndView addMv = new ModelAndView("/HotelRoom/RoomType/add");
        return addMv;
    }

    //    添加用户页面用户信息添加
    @PostMapping("/add")
    public R add(@RequestBody RoomType roomType) {
        int save = roomTypeService.saveRoomType(roomType);
        if (save > 0) {
            return R.ok("添加成功");
        } else {
            return R.fail("添加失败");
        }
    }


    //   删除功能，逻辑删除，将isdel字段改为true
    @PostMapping("/delete")
    public R delete(@RequestBody List<Integer> ids) {
        List<RoomType> list = new ArrayList<>();
        for (Integer id : ids) {
            RoomType roomType = new RoomType();
            roomType.setId(id);
//            根据id查询出来的数据，将isdel字段改为true
            roomType.setIsDel(true);
            list.add(roomType);
        }
        boolean b = roomTypeService.updateBatchById(list);
        if (b) {
            return R.ok("删除成功");
        } else {
            return R.fail("删除失败");
        }
    }
    @GetMapping("/recover")
    public ModelAndView recover() {
        ModelAndView recover = new ModelAndView("/HotelRoom/RoomType/recover");
        return recover;
    }
    @GetMapping("/recoverList")
    public R recover(@RequestParam(defaultValue = "1") Integer page,
                     @RequestParam(defaultValue = "5") Integer limit, RoomType roomType) {
        PageInfo<RoomType> pageInfo = roomTypeService.selectByIsdel(roomType, page, limit);
        return R.ok(pageInfo);
    }
    //   恢复功能，逻辑删除，将isdel字段改为false
    @PostMapping("/recover")
    public R recover(@RequestBody List<Integer> ids) {
        List<RoomType> list = new ArrayList<>();
        for (Integer id : ids) {
            RoomType roomType = new RoomType();
            roomType.setId(id);
//            根据id查询出来的数据，将isdel字段改为true
            roomType.setIsDel(false);
            list.add(roomType);
        }
        boolean b = roomTypeService.updateBatchById(list);
        if (b) {
            return R.ok("恢复成功 ");
        } else {
            return R.fail("恢复失败");
        }
    }
}
